﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using STeaL.Collections.ObjectModel;
using STeaL.Collections.Generic;
using STeaL.ComponentModel;

namespace TwoDictionaries {

  class BindData : INotifyPropertyChanged {

    public BindData() {
      LeftDic = new Dictionary();
      RightDic = new ObservableCollection<Entry>();
      left_ = LeftDic; 
      right_ = new multiset_adaptor<Entry>(RightDic,(x,y)=>x.Key.CompareTo(y.Key)<0);
      LeftStatus = "message shown here";
      RightStatus = "message shown here";
    }

    public void addEntry(string k, string v) {
      Entry entry = new Entry() { Key = k, Value=v };
      try {
        left_.Add(entry);
        LeftStatus = "sucessfully added";
      } catch ( Exception ex ) {
        LeftStatus = ex.Message;
      }
      PropertyChanged.Notify(this,"LeftStatus");
      try {
        right_.Add(entry);
        RightStatus = "sucessfully added";
      } catch ( Exception ex ) {
        RightStatus = ex.Message;
      } 
      PropertyChanged.Notify(this,"RightStatus");
    }

    public void removeEntry(string k) {
      Entry entry = new Entry() { Key = k, Value=string.Empty };
      try {
        left_.Remove(k);
        LeftStatus = "sucessfully removed";
      } catch ( Exception ex ) {
        LeftStatus = ex.Message;
      } 
      PropertyChanged.Notify(this,"LeftStatus");
      try {
        right_.Remove(entry);
        RightStatus = "sucessfully removed";
      } catch ( Exception ex ) {
        RightStatus = ex.Message;
      } 
      PropertyChanged.Notify(this,"RightStatus");
    }

    public Dictionary                  LeftDic  { get; private set; }
    public ObservableCollection<Entry> RightDic { get; private set; }
    public string LeftStatus  { get; private set; }
    public string RightStatus { get; private set; }

    private Dictionary                 left_;
    private multiset_adaptor<Entry>    right_;

    public event PropertyChangedEventHandler PropertyChanged;
  }

  class Entry {
    public string Key { get; set; }
    public string Value { get; set; }
  }

  class Dictionary : ObservableKeyedCollection<string, Entry> {
    protected override string GetKeyForItem(Entry item) { return item.Key; }
  }
}
